package com.deeper.customview.ui.c6.exercise.c06e02;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 瀑布流，固定为3列
 */
public class WaterfallLayout extends ViewGroup {

    public WaterfallLayout(Context context) {
        super(context);
    }


    public WaterfallLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public WaterfallLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }


    private int measureWidth(int widthMeasureSpec) {
        int width = 0;
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            width = size;
        }
        return width;
    }


    private int measureHeight(int heightMeasureSpec) {
        int height = 0;
        //这里固定为3列等宽
        int row1Height = 0;
        int row2Height = 0;
        int row3Height = 0;

        int n = getChildCount();
        for (int i = 0; i < n; i++) {
            View child = getChildAt(i);
            if (Math.min(row1Height, row2Height) == row1Height
                    && Math.min(row1Height, row3Height) == row1Height) {
                //row1Height 最小，当前子组件放在这里
                row1Height += child.getMeasuredHeight();

            } else if (Math.min(row1Height, row2Height) == row2Height
                    && Math.min(row2Height, row3Height) == row2Height) {
                //row2Height 最小，当前子组件放在这里
                row2Height += child.getMeasuredHeight();

            } else if (Math.min(row1Height, row3Height) == row3Height
                    && Math.min(row2Height, row3Height) == row3Height) {
                //row3Height 最小，当前子组件放在这里
                row3Height += child.getMeasuredHeight();

            }
            height = Math.max(row1Height, Math.max(row2Height, row3Height));
        }

        return height;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int totalWidth = getMeasuredWidth();

        //这里固定为3列等宽
        int rowWidth = totalWidth / 3;
        int row1Left = 0;
        int row2Left = rowWidth;
        int row3Left = rowWidth * 2;
        //每列当前高度
        int row1Height = 0;
        int row2Height = 0;
        int row3Height = 0;

        int n = getChildCount();
        for (int i = 0; i < n; i++) {
            View child = getChildAt(i);

            if (Math.min(row1Height, row2Height) == row1Height
                    && Math.min(row1Height, row3Height) == row1Height) {
                //row1Height 最小，当前子组件放在这里
                child.layout(row1Left, row1Height, row1Left + rowWidth, row1Height + child.getMeasuredHeight());

                row1Height += child.getMeasuredHeight();

            } else if (Math.min(row1Height, row2Height) == row2Height
                    && Math.min(row2Height, row3Height) == row2Height) {
                //row2Height 最小，当前子组件放在这里
                child.layout(row2Left, row2Height, row2Left + rowWidth, row2Height + child.getMeasuredHeight());

                row2Height += child.getMeasuredHeight();

            } else if (Math.min(row1Height, row3Height) == row3Height
                    && Math.min(row2Height, row3Height) == row3Height) {
                //row3Height 最小，当前子组件放在这里
                child.layout(row3Left, row3Height, row3Left + rowWidth, row3Height + child.getMeasuredHeight());

                row3Height += child.getMeasuredHeight();
            }
        }
    }
}
